package com.woniuxy01;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 作者
 * @create 2025-09-24 14:03
 */
public class Test03 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Student.class;
//        clazz.newInstance();
        //获取所有构造方法
//        Constructor[] constructors = clazz.getDeclaredConstructors();
//        for (Constructor constructor : constructors) {
//            System.out.println(constructor);
//        }
        //使用构造方法创建对象
        Constructor constructor = clazz.getConstructor();
        Student student = (Student) constructor.newInstance();
        System.out.println(student);

        Constructor constructor1 = clazz.getConstructor(Integer.class, String.class, Integer.class, String.class);
        Student student1 = (Student) constructor1.newInstance(1, "a", 11, "aa");
        System.out.println(student1);

    }


    private static void test02() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        Method method = clazz.getMethod("test01");
        //filedName.set(student,"zs");
        method.invoke(student);

        Method method1 = clazz.getMethod("test01", int.class);
        //student.test01(2);
        method1.invoke(student, 2);

        Method method2 = clazz.getMethod("test01", int.class, String.class);
        //student.test01(1,"aaaa");
        String str = method2.invoke(student, 1, "xxxx").toString();
        System.out.println(str);


        //调用私有方法，设置暴力访问
        Method method3 = clazz.getDeclaredMethod("test02");
        method3.setAccessible(true);
        method3.invoke(student);
    }

    private static void test01() throws InstantiationException, IllegalAccessException, NoSuchMethodException {
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        //getMethods()获取所有public方法
//        Method[] methods = clazz.getMethods();
//        for (Method method : methods) {
//            System.out.println(method);
//        }
        //getDeclaredMethods()获取所有方法
//        Method[] methods = clazz.getDeclaredMethods();
//        for (Method method : methods) {
//            System.out.println(method);
//        }

        //获取test01()无参方法
        Method method = clazz.getMethod("test01");
        System.out.println(method);
        //获取test01(int xxx)无参方法
        Method method1 = clazz.getMethod("test01", int.class);
        System.out.println(method1);
        //获取test01(int xxx,String yyy)无参方法
        Method method2 = clazz.getMethod("test01", int.class, String.class);
        System.out.println(method2);

        Method method3 = clazz.getDeclaredMethod("test02");
        System.out.println(method3);
    }
}
